package com.example.myapplication.tack_05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_05.data.SetPassWordData;
import com.example.myapplication.utils.OkHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class Feedback extends AppCompatActivity {
    private ImageView exit;
    private EditText title;
    private EditText content;
    private LinearLayout save;
    private JSONObject jsonObject;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_05_feedback);
        init();
        exit_();
        callkSave();
    }

    private void callkSave() {
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = content.getText().toString();
                String str2 = title.getText().toString();

                try {
                    jsonObject.put("content",str1);
                    jsonObject.put("title",str2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(str2.length() >= 200){
                            Toast.makeText(Feedback.this,"反馈长度限制200字",Toast.LENGTH_SHORT).show();
                        }
                        String json = jsonObject.toString();
                        getFeedback(json);
                    }
                });
            }
        });
    }

    private void getFeedback(String json) {
        String url = "prod-api/api/common/feedback";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, SetPassWordData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                SetPassWordData setPassWordData = (SetPassWordData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        feedbackAdapter(setPassWordData);
                        if(setPassWordData.getCode() == 200){
                            Toast.makeText(Feedback.this,setPassWordData.getMsg(),Toast.LENGTH_SHORT).show();
                            Feedback.this.finish();
                        }else {
                            Toast.makeText(Feedback.this,setPassWordData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void feedbackAdapter(SetPassWordData setPassWordData) {

    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Feedback.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        save = findViewById(R.id.save);
        jsonObject = new JSONObject();
    }
}